package com.wangyonghao.harmonyocrs.dao;

import com.wangyonghao.harmonyocrs.pojo.User;
import com.wangyonghao.harmonyocrs.utils.DateUtils;
import com.wangyonghao.harmonyocrs.utils.HttpUtils;
import ohos.data.DatabaseHelper;
import ohos.data.rdb.RdbOpenCallback;
import ohos.data.rdb.RdbStore;
import ohos.data.rdb.StoreConfig;
import ohos.data.rdb.ValuesBucket;
import ohos.data.resultset.ResultSet;

import java.util.HashMap;
import java.util.Map;

public class UserDao {
    DatabaseHelper helper;
    RdbStore store;
    StoreConfig config = StoreConfig.newDefaultConfig("HarmonyCORS.db");
    HttpUtils httpUtils = new HttpUtils();
    private static RdbOpenCallback callback = new RdbOpenCallback() {
        @Override
        public void onCreate(RdbStore rdbStore) {
            rdbStore.executeSql("create table if not exists user " +
                    "(id integer primary key autoincrement,account text," +
                    "password text, name text,create_time text,last_update_time text)");
            rdbStore.executeSql("insert into user (id,account,password,name,create_time,last_update_time)" +
                    "values(1001,'test','123','张三','2022-02-22 21:13:14','2022-02-22 21:13:14')");
            rdbStore.executeSql("insert into user (id,account,password,name,create_time,last_update_time)" +
                    "values(1002,'ls','123','李四','2022-02-22 21:13:14','2022-02-22 21:13:14')");
        }

        @Override
        public void onUpgrade(RdbStore rdbStore, int i, int i1) {

        }
    };

    public UserDao(DatabaseHelper helper) {
        this.helper = helper;
        store = helper.getRdbStore(config, 1, callback, null);
    }

    public long addUser(User user) {
        ValuesBucket bucket = new ValuesBucket();
        bucket.putInteger("id", user.getId());
        bucket.putString("account", user.getAccount());
        bucket.putString("password", user.getPassword());
        bucket.putString("name", user.getName());
        bucket.putString("create_time", DateUtils.datetimeToString(DateUtils.now()));
        bucket.putString("lastUpdate_time", DateUtils.datetimeToString(DateUtils.now()));
        return store.insert("user", bucket);
    }

    public User login(String account, String password) {
        ResultSet resultSet = store.querySql("select id,account,password,name,create_time,last_update_time from user where account = ? and password = ?", new String[]{account, password});
        resultSet.goToFirstRow();
        if (resultSet.isEnded()) {
            return null;
        }
        User user = new User();
        user.setId(resultSet.getInt(0));
        user.setAccount(resultSet.getString(1));
        user.setPassword(resultSet.getString(2));
        user.setName(resultSet.getString(3));
        user.setCreateTime(DateUtils.stringToDate(resultSet.getString(4)));
        user.setLastUpdateTime(DateUtils.stringToDate(resultSet.getString(5)));
        return user;
    }

    public User findUserByName(String name) {
        ResultSet resultSet = store.querySql("select id,account,password,name,create_time,last_update_time from user where name = ?", new String[]{name});
        resultSet.goToFirstRow();
        User user = new User();
        user.setId(resultSet.getInt(0));
        user.setAccount(resultSet.getString(1));
        user.setPassword(resultSet.getString(2));
        user.setName(resultSet.getString(3));
        user.setCreateTime(DateUtils.stringToDate(resultSet.getString(4)));
        user.setLastUpdateTime(DateUtils.stringToDate(resultSet.getString(5)));
        return user;
    }

    public User findUserById(Integer id) {
        ResultSet resultSet = store.querySql("select id,account,password,name,create_time,last_update_time from user where id = ?", new String[]{String.valueOf(id)});
        resultSet.goToFirstRow();
        User user = new User();
        user.setId(resultSet.getInt(0));
        user.setAccount(resultSet.getString(1));
        user.setPassword(resultSet.getString(2));
        user.setName(resultSet.getString(3));
        user.setCreateTime(DateUtils.stringToDate(resultSet.getString(4)));
        user.setLastUpdateTime(DateUtils.stringToDate(resultSet.getString(5)));
        return user;
    }
}
